package igou.togogo.controller;

import igou.togogo.bean.EBrand;
import igou.togogo.domain.ProductDetail;
import igou.togogo.domain.ProductResult;
import igou.togogo.page.Pagination;
import igou.togogo.service.IBrandService;
import igou.togogo.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//前台 首页展示
@Controller
public class IndexController {

    @Autowired
    IBrandService brandService;

    @Autowired
    IProductService productService;

    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request) {
        //查询品牌
        List<EBrand> list = brandService.getBrandList();
        request.getSession().setAttribute("brandList", list);
        model.addAttribute("brandList", list);

        //查询商品
        ProductResult result = productService.getProductByBrandId(-1, 1, 8);
        List<ProductDetail> productList = result.getList();
        Pagination pagination = result.getPagination();
        String url = "/product/list.action";
        StringBuilder params = new StringBuilder("brandId=" + -1);
        params.append("&rows=" + 8);
        pagination.pageView(url, params.toString());
        model.addAttribute("brandId", -1);
        model.addAttribute("productList", productList);
        model.addAttribute("pagination", pagination);
        return "front_page/product/product";
    }
}
